package com.melorour.product.controller;

import com.melorour.product.CartDTOInput;
import com.melorour.product.ProductInfoOutput;
import com.melorour.product.dto.CartDTO;
import com.melorour.product.entity.ProductCategory;
import com.melorour.product.entity.ProductInfo;
import com.melorour.product.enums.ResultEnum;
import com.melorour.product.exception.ProductException;
import com.melorour.product.service.CategoryService;
import com.melorour.product.service.ProductService;
import com.melorour.product.utils.ResultVOUtil;
import com.melorour.product.vo.ProductInfoVO;
import com.melorour.product.vo.ProductVO;
import com.melorour.product.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Melorour on 2018/4/13.
 */
@RestController
@RequestMapping("/product")
public class ProdcutController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public ResultVO list() {
        List<ProductInfo> productInfoList = productService.findUpAll();
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCategoryType).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo: productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }
        return ResultVOUtil.success(productVOList);
    }

    @PostMapping("listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {
        /*try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        List<ProductInfo> productInfoList = productService.findByProductIn(productIdList);
        List<ProductInfoOutput> productInfoOutputs = new ArrayList<>();
        for (ProductInfo productInfo : productInfoList) {
            ProductInfoOutput productInfoOutput = new ProductInfoOutput();
            BeanUtils.copyProperties(productInfo,productInfoOutput);
            productInfoOutputs.add(productInfoOutput);
        }
        return productInfoOutputs;
    }

    @PostMapping("decreaseStock")
    public void decreaseStock(@RequestBody List<CartDTOInput> cartDTOInputList) {
        if (CollectionUtils.isEmpty(cartDTOInputList)) {
            throw new ProductException(ResultEnum.CART_EMPTY);
        }
        List<CartDTO> cartDTOList = cartDTOInputList.stream().map(e->new CartDTO(e.getProductId(),e.getProductQuantity()))
                .collect(Collectors.toList());
        productService.decreaseStock(cartDTOList);
    }
}
